package com.data.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.base.utils.RandomUtils;
import com.config.dao.EquipmentDao;
import com.config.dao.LineConfigDao;
import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.data.dao.BaseDataDao;
import com.data.dao.BaseDataDtoDao;
import com.data.entity.BaseData;
import com.data.entity.BaseDataDto;

@Service("baseDataService")
public class BaseDataServiceImpl  implements BaseDataService{
	@Resource(name="baseDataDao")
	private  BaseDataDao baseDataDao;
	@Resource(name="lineConfigDao")
	private LineConfigDao lineConfigDao;
	@Resource(name="equipmentDao")
	private EquipmentDao equipmentDao;
	@Resource(name="baseDataDtoDao")
	private   BaseDataDtoDao baseDataDtoDao;
	private boolean firstTime=true;
	/**
	 * 利用随机数据生成模拟数据
	 */
	@Override
	public void executeCodeSavaData() {
		List<LineConfig> lines=lineConfigDao.findAllLine();
		if(lines.size()!=0){
			for(LineConfig line:lines){
				this.simulateData(line.getLinesNo());
    			List<Equipment> equs=equipmentDao.listByLine(line.getId());
    			if(equs.size()!=0){
    				for(Equipment e:equs){
    					this.simulateData(e.getEquNo());
    				}
    			}
			}
		}
    }
	/**
	 * 根据生产线或者设备编号生成每秒的模拟数据
	 * @param equNo
	 */
	private void simulateData(String equNo){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		BaseData bd=new BaseData();
		bd.setEquNo(equNo);
		bd.setU_a(RandomUtils.randoms(500));
//		bd.setU_b(RandomUtils.randomSmall(1000));
//		bd.setU_c(RandomUtils.randomSmall(1000));
//		bd.setU_ab(RandomUtils.randomSmall(1000));
//		bd.setU_bc(RandomUtils.randomSmall(1000));
//		bd.setU_ca(RandomUtils.randomSmall(1000));
		bd.setI_a(RandomUtils.randoms(400));
//		bd.setI_b(RandomUtils.randomSmall(500));
//		bd.setI_c(RandomUtils.randomSmall(500));
//		bd.setP_a_active(RandomUtils.randomBig(2000));
//		bd.setP_b_active(RandomUtils.randomBig(2000));
//		bd.setP_c_active(RandomUtils.randomBig(2000));
		bd.setP_active(RandomUtils.randoms(500));
//		bd.setP_a_reactive(RandomUtils.randomSmall(800));
//		bd.setP_b_reactive(RandomUtils.randomSmall(800));
//		bd.setP_c_reactive(RandomUtils.randomSmall(800));
//		bd.setP_reactive(RandomUtils.randomSmall(1500));
//    bd.setP_a_apparent(RandomUtils.randomBig(1200));
//    bd.setP_b_apparent(RandomUtils.randomBig(1200));
//    bd.setP_c_apparent(RandomUtils.randomBig(1200));
//    bd.setP_apparent(RandomUtils.randomBig(2500));
//    bd.setP_a_factor(RandomUtils.randomSmall(100));
//    bd.setP_b_factor(RandomUtils.randomSmall(100));
//    bd.setP_c_factor(RandomUtils.randomSmall(100));
//    bd.setP_factor(RandomUtils.randomSmall(200));
        bd.setFrequency(1);
        if(firstTime){
        	 firstTime=false;
        		//最后一条记录
        		BaseDataDto lastbd=baseDataDtoDao.getLastBaseDataDto(equNo);
        		if(lastbd!=null){
        			bd.setP_engry(lastbd.getP_engry()+RandomUtils.randoms(10));
        		}else{
        			bd.setP_engry(RandomUtils.randomBig(1000));
        		}
        }else{
                //查询离前一秒最近的时间点的记录
                BaseDataDto bdata=baseDataDtoDao.getBaseDataDtoByEquNoAndDate(equNo, sdf.format(new Date().getTime()-1000));
                if(bdata!=null){
                	 bd.setP_engry(bdata.getP_engry()+RandomUtils.randoms(10));
                }else{
                     bd.setP_engry(RandomUtils.randomBig(1000));
                }
        	}
//        bd.setN_engry(RandomUtils.randomSmall(1000));
//        bd.setI_power(RandomUtils.randomSmall(1800));
//        bd.setC_power(RandomUtils.randomSmall(1500));
        bd.setTime(new Date());
        baseDataDao.save(bd);
	}
	
	/**
	 * 获得第一条记录
	 */
	@Override
	public BaseData getFirstBaseData() {
		return baseDataDao.getFirstBaseData();
	}
}
